import 'package:fish_redux/fish_redux.dart';
import 'action.dart';
import 'state.dart';
import '../../../app_files.dart';
import 'dart:io';
import 'package:date_format/date_format.dart';

Effect<UserEditInfoViewState> buildEffect() {
  return combineEffects(<Object, Effect<UserEditInfoViewState>>{
    UserEditInfoViewAction.onTap: _onTap,
    UserEditInfoViewAction.onSubmit: _onSubmit,
    Lifecycle.initState: _initState,
  });
}

void _initState(Action action, Context<UserEditInfoViewState> ctx) async {
  UserInfoEntity info = GlobalStore.store.getState().userInfo;
  ctx.state.userHead = info.headimg;
  ctx.state.nickName = info.nickname;
  ctx.state.sex = info.gender;
}

void _onSubmit(Action action, Context<UserEditInfoViewState> ctx) async {
  CommonUtils.showLoadingDialog(ctx.context);
  UserInfoEntity info = GlobalStore.store.getState().userInfo;
  String header = info.headimg;
  if (ctx.state.selectImage != null) {
    ResultData res =
        await CommonNetWork.uploadImg(ctx.state.selectImage);
    if (res.success) {
      header = res.data;
    }
  }
  ResultData res = await CommonNetWork.setUserInfo(
      nickName: ctx.state.nickName, sex: ctx.state.sex, header: header);
  Navigator.pop(ctx.context);
  if (res.success) {
    info.nickname = ctx.state.nickName;
    info.gender = ctx.state.sex;
    info.headimg = header;
    GlobalStore.store.dispatch(GlobalActionCreator.onUserLogin(info));
    await CommonUtils.showAlertView(ctx.context, '保存成功');
    Navigator.pop(ctx.context);
  } else {
    CommonUtils.showErrorInfo(ctx.context, res.desc);
  }
}

void _onTap(Action action, Context<UserEditInfoViewState> ctx) async {
  print(action.payload);
  switch (action.payload) {
    case '头像':
      File imgFile =
          await CommonUtils.showImagePicker(ctx.context, cropImg: true);
      if (imgFile == null) {
        return;
      }
      ctx.dispatch(UserEditInfoViewActionCreator.onSelectHeader(imgFile));
//      CommonUtils.showWaitLoading(ctx.context);
//      var res = await CommonNetWork.uploadImg(imgFile);
//      Navigator.pop(ctx.context);
//      if (res.success) {
//        ctx.state.userInfo.headimg = res.data['url'];
//      } else {
//        CommonUtils.showToast(ctx.context, res.desc,
//            iconType: ToastTipsIconType.failed);
//      }
      break;
    case '昵称':
      var res = await CommonUtils.pushRoute(ctx.context, RoutePath.editInfoView,
          arguments: {
            'title': action.payload,
            'value': ctx.state.nickName,
          });
      if (res != null) {
        ctx.state.nickName = res;
        ctx.dispatch(UserEditInfoViewActionCreator.onAction());
      }
      break;
//    case '联系方式':
//      var res = await CommonUtils.pushRoute(ctx.context, RoutePath.editInfoView,
//          arguments: {
//            'title': action.payload,
//            'value': ctx.state.userInfo.mobile,
//          });
//      if (res != null) {
//        ctx.state.userInfo.mobile = res;
//        ctx.dispatch(UserEditInfoViewActionCreator.onAction());
//      }
//      break;
//    case '职业':
//      var res = await CommonUtils.pushRoute(ctx.context, RoutePath.editInfoView,
//          arguments: {
//            'title': action.payload,
//            'value': ctx.state.work,
//          });
//      if (res != null) {
//        ctx.state.work = res;
//        ctx.dispatch(UserEditInfoViewActionCreator.onAction());
//      }
//      break;
    case '性别':
      CommonUtils.showActionSheet(ctx.context, button: ['男', '女'])
          .then((value) async {
        if (value != -1 && value != null) {
          ctx.state.sex = value == 0 ? false : true;
          ctx.dispatch(UserEditInfoViewActionCreator.onAction());
        }
      });
      break;
//    case '生日':
//      DateTime time = DateTime(1990, 01, 01);
//      try {
//        ///1994/01/01
//        if (ctx.state.birthday.length == 0) {
//          time = DateTime(1990, 01, 01);
//        } else {
//          time = DateTime(
//              int.parse(ctx.state.birthday.substring(0, 4)),
//              int.parse(ctx.state.birthday.substring(5, 7)),
//              int.parse(ctx.state.birthday.substring(8, 10)));
//        }
//      } catch (e) {
//        time = DateTime(1990, 01, 01);
//      }
//
//      CommonUtils.showCupertinoDatePicker(ctx.context, time).then((date) async {
//        if (date == null) return;
//        String _birthday = formatDate(date, [yyyy, '-', mm, '-', dd]);
//        ctx.state.birthday = _birthday;
//        ctx.dispatch(UserEditInfoViewActionCreator.onAction());
//      });
//      break;
  }
}
